package com.softeem.test;

public class ThreadLocaDemo {
 
    private static ThreadLocal<String> localVar = new ThreadLocal<>();
 
    public static void print(String str) {
        //打印当前线程中本地内存中本地变量的值
        System.out.println(str + " :" + localVar.get());
        //清除本地内存中的本地变量
        localVar.remove();
    }

    public static void main(String[] args) throws InterruptedException {
 
        new Thread(new Runnable() {
            public void run() {
                localVar.set("local_A");
                print("A");//A : local_A
                //打印本地变量
                System.out.println("after remove : " + localVar.get());//after remove : null

            }
        }).start();
 
        Thread.sleep(1000);//休眠一秒
 
        new Thread(new Runnable() {
            public void run() {
                localVar.set("local_B");
                print("B");//B : local_B
                System.out.println("after remove : " + localVar.get());//after remove : null
              
            }
        }).start();
    }
}
 
//A :local_A
//after remove : null
//B :local_B
//after remove : null
 